﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class TimeTableViewModel : INotifyPropertyChanged
    {
        public TimeTableViewModel()
        {
            RefreshEvents();
            this.PropertyChanged += (s, e) => {
                if (insertCurPhong != null)
                    insertCurPhong.RaiseCanExecuteChanged();
                if (saveCurPhong != null)
                    saveCurPhong.RaiseCanExecuteChanged();
                if (delCurPhong != null)
                    delCurPhong.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<PHONG> phongs;
        public IEnumerable<PHONG> Phongs
        {
            get { return this.Phongs; }
            set
            {
                this.Phongs = value;
                OnPropertyChange("Phongs");
            }
        }


        private PHONG currentPhong;
        public PHONG CurrentPhong
        {
            get { return this.currentPhong; }
            set
            {
                this.currentPhong = value;
                OnPropertyChange("CurrentPhong");
                RefreshCompetitor();
            }
        }
        private IEnumerable<LOAIPHONG> loaiphongs;
        public IEnumerable<LOAIPHONG> Loaiphongs
        {
            get { return this.loaiphongs; }
            set
            {
                this.loaiphongs = value;
                OnPropertyChange("Loaiphongs");
            }
        }
        
        private LOAIPHONG currentLoaiPhong = new LOAIPHONG();
        public LOAIPHONG CurrentLoaiPhong 
        {
            get { return this.currentLoaiPhong; }
            set
            {
                this.currentLoaiPhong = value;
                OnPropertyChange("CurrentLoaiPhong");
            }
        }
       
        #endregion

        #region refresh
        private void RefreshEvents()
        {
            this.ServiceClient.getAllPhongCompleted += (s, e) =>
            {
                this.Phongs = e.Result;
            };
            this.ServiceClient.getAllPhongAsync();
        }

        private void RefreshCompetitor()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllLoaiPhongCompleted += (s, e) =>
            {
                this.loaiphongs = e.Result;
            };
            this.ServiceClient.getAllLoaiPhongAsync();
            

        }
        #endregion
        
        #region Save
        private RelayCommand saveCurPhong;
        public ICommand SaveCurPhong
        {
            get { return saveCurPhong ?? (saveCurPhong = new RelayCommand(() => SavePhong(), () => this.currentPhong != null && this.currentPhong.ID != 0)); }
        }
        
        private void SavePhong()
        {
            ServiceClient.savePhongAsync(currentPhong);
            RefreshCompetitor();
        }

        
        #endregion

        #region del
        private RelayCommand delCurPhong;
        public ICommand DelCurPhong
        {
            get { return delCurPhong ?? (delCurPhong = new RelayCommand(() => DelPhong(), () => this.currentPhong != null && this.currentPhong.ID != 0)); }
        }

        private void DelPhong()
        {
            ServiceClient.deletePhongAsync(currentPhong);
            RefreshCompetitor();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurPhong;
        public ICommand InsertCurPhong
        {
            get { return insertCurPhong ?? (insertCurPhong = new RelayCommand(() => InsertPhong(), () => this.currentPhong != null)); }
        }

        private void InsertPhong()
        {
            ServiceClient.insertPhongAsync(CurrentPhong);
            RefreshCompetitor();
        }
        #endregion
        
    }
}
